package com.omesoft.hksnore.util.myactivity;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;

import com.omesoft.hksnore.R;
import com.omesoft.hksnore.monitoring.MonitorFragment;
import com.omesoft.hksnore.util.CustomToast;
import com.omesoft.hksnore.util.omeview.MyDialog;
import com.umeng.analytics.MobclickAgent;

/**
 * 基类
 */
public class MyFragmentActivity extends FragmentActivity {
    protected Context context;
    protected Activity activity;
    protected Handler handler1;
    private final static String TAG = "MyFragmentActivity";
    protected int anim = -1;// 跳转动画选择
    private Dialog confirmDialog;

    protected Handler handler;

    // 是否能够退出
    private boolean isBack = false;
    // 上次按退出的时间
    private long downTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
        activity = this;
        context = this;
        MobclickAgent.setDebugMode(true);
        // SDK在统计Fragment时，需要关闭Activity自带的页面统计，
        // 然后在每个页面中重新集成页面统计的代码(包括调用了 onResume 和 onPause 的Activity)。
        MobclickAgent.openActivityDurationTrack(false);
        // MobclickAgent.setAutoLocation(true);
        // MobclickAgent.setSessionContinueMillis(1000);
        // MobclickAgent.startWithConfigure(
        // new UMAnalyticsConfig(mContext, "4f83c5d852701564c0000011", "Umeng",
        // EScenarioType.E_UM_NORMAL));
        MobclickAgent.setScenarioType(context, MobclickAgent.EScenarioType.E_UM_NORMAL);
    }


    @SuppressWarnings("static-access")
    protected void init() {
    }

    protected void initTitleBar() {

    }

    protected void initView() {

    }

    protected void loadView() {

    }

    protected void initHandler() {
        handler = new Handler() {

            @Override
            public void dispatchMessage(Message msg) {
                super.dispatchMessage(msg);
            }
        };
    }

    @Override
    protected void onResume() {
        Log.e("mye", "onResume...MyFragmentActivity...");
        super.onResume();
    }

    @Override
    protected void onPause() {
        Log.e("mye", "onPause....MyFragmentActivity..");
        super.onPause();
    }

    protected void sendMsg(int whatId, Object objs) {
        Message msg = new Message();
        msg.what = whatId;
        msg.obj = objs;
        Log.v("sendMsg", "msg.what=" + msg.what);
        if (handler != null) {
            handler.sendMessage(msg);
        }
    }

    protected void loadAnim() {
        overridePendingTransition(R.anim.in_lefttoright, R.anim.out_lefttoright);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (!MonitorFragment.isMonitor) {

                    if (!isBack) {
                        CustomToast.showToasts(context, R.string.snore_main_exit);
                        downTime = event.getDownTime();
                        isBack = true;
                        return true;
                    } else {
                        if (event.getDownTime() - downTime <= 2000) {
                            android.os.Process.killProcess(android.os.Process.myPid());
                        } else {

                            CustomToast.showToasts(context, R.string.snore_main_exit);
                            downTime = event.getDownTime();

                            return true;
                        }
                    }
                }else {
                    final MyDialog.Builder builder = new MyDialog.Builder(this);
                    builder.setTitle(R.string.snore_main_moniting_exit);
                    builder.setPositiveButton(getString(R.string.snore_main_btn_sure),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface arg0, int arg1) {
                                    confirmDialog.dismiss();
                                    android.os.Process.killProcess(android.os.Process.myPid());

                                }
                            });
                    builder.setNegativeButton(getString(R.string.snore_main_btn_cancel),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface arg0, int arg1) {
                                    confirmDialog.dismiss();
                                }
                            });
                    confirmDialog = builder.create();
                    confirmDialog.show();
                }
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void finish() {
        super.finish();

        loadAnim();

    }

}

